﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;

namespace TaxiDispatchClient
{
    public partial class CallRecordsForm : DevExpress.XtraEditors.XtraForm
    {
        public CallRecordsForm()
        {
            InitializeComponent();                       
        }
        private void CallRecordsForm_Load(object sender, EventArgs e)
        {
            dateEdit_dateStart.DateTime = DateTime.Now.AddDays(-1);
            dateEdit_dateEnd.DateTime = DateTime.Now;            
            
            _isLoad = true;
            _thread = new Thread(BindData);
            _thread.Start();
            simpleButton_select.Enabled = false;
        }
        private Thread _thread;
        //private int _pageCount;     //总计多少页
        //private int _currentPage;  //当前页码
        //private int _pagesize = 20;    //每页显示数量
        //private int _recordCount; // 数据总条数，即客户总数量
        private bool _isLoad;  // 是否为加载  用于判断是否设置grid样式
        //private bool _isSelect;

        private void BindData()
        {           
            string timeStart = dateEdit_dateStart.DateTime.Date.ToString("yyyy-MM-dd") + " "
              + "00:00:00";
            string timeEnd = dateEdit_dateEnd.DateTime.Date.ToString("yyyy-MM-dd") + " "
                +"23:59:59"; 

            string sql = "calltime between '" + timeStart + "' and '" + timeEnd+"'";
            if(combox_type.SelectedIndex==1)
            {
                sql += " and type='0'";
            }
            if (combox_type.SelectedIndex == 2)
            {
                sql += " and type='1'";
            }
            try
            {
                DataSet ds = MainForm.PositionService.CallRecordsGetList(sql);
                if(ds!=null)
                {
                    BeginInvoke(new MethodInvoker(() =>
                    {
                        gridControl_list.DataSource = ds.Tables[0];
                        gridView1.GroupPanelText = "共计" +
                            ds.Tables[0].Rows.Count + "条记录";
                        simpleButton_select.Enabled = true;
                    }));

                    if (_isLoad)
                    {
                        SetGridStyle();
                        _isLoad = false;
                    } 
                }               
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("正在中止线程"))
                {
                    Common.CommonRoutine.HandlingExceptions(ex);
                }
                else
                {
                    Common.CommonRoutine.HandlingExceptions(ex, true);
                    simpleButton_select.Enabled = true;
                }        
            }                                    
        }

        private void SetGridStyle()
        {
            BeginInvoke(new MethodInvoker(() =>
            {
                gridView1.OptionsBehavior.Editable = false;
                gridView1.Columns["Id"].Visible = false;
                gridView1.Columns["phoneNumber"].Caption = "电话号码";
                gridView1.Columns["phoneNumber"].AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
                gridView1.Columns["phoneNumber"].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
                gridView1.Columns["type"].Caption = "来电/去电";
                gridView1.Columns["type"].AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
                gridView1.Columns["type"].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
                gridView1.Columns["calltime"].AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
                gridView1.Columns["calltime"].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
                gridView1.Columns["calltime"].Caption = "来电时间";    
            }));            
        }


        private void simpleButton_select_Click(object sender, EventArgs e)
        {        
            if(dateEdit_dateStart.DateTime>dateEdit_dateEnd.DateTime)
            {
                XtraMessageBox.Show("查询开始日期不能大于截止日期！");
                dateEdit_dateStart.Focus();
                return;
            }            
            _thread = new Thread(BindData);
            _thread.Start();
            simpleButton_select.Enabled = false;
            gridControl_list.Focus();
        }


        #region 窗体效果
        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;
            Cursor = Cursors.Arrow;
        }
        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }


        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void Form_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }

        #endregion

        private void btn_exportToExcel_Click(object sender, EventArgs e)
        {
            //string timeStart = dateEdit_start.DateTime.ToString("yyyy年MM月dd号HH点mm分");
            //string timeEnd = dateEdit_end.DateTime.ToString("yyyy年MM月dd号HH点mm分");
            string timeStart = dateEdit_dateStart.DateTime.Date.ToString("yyyy年MM月dd号");
            string timeEnd = dateEdit_dateEnd.DateTime.Date.ToString("yyyy年MM月dd号"); 

            saveFileDialog1.FileName = timeStart+"至"+timeEnd+ "来电记录";
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }

        private void CallRecordsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_thread != null)
            {
                _thread.Abort();
            }
        }

        
    }
}